//
//  TipToolBar.m
//  UIScrollView
//
//  Created by Sang-don Lee on 11. 11. 10..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "TipToolBar.h"

@implementation TipToolBar

- (id)init
{
    self = [super init];
    self.frame = [self frameForTipTollBar];
    
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.7f;
    
    [self addSubview:backgroundView];
    [backgroundView release];
    
    tipCloseButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 60, 0, 60, 30)];
    tipCloseButton.backgroundColor = [UIColor whiteColor];
    [tipCloseButton addTarget:self action:@selector(showAndHide:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:tipCloseButton];
    [tipCloseButton release];
    
    openFrame = [self frameForTipTollBar];
    closeFrame = CGRectMake(openFrame.origin.x, [[UIScreen mainScreen] applicationFrame].size.height - tipCloseButton.frame.size.height, self.frame.size.width, self.frame.size.height);
    
    textView = [[UITextView alloc] initWithFrame:CGRectMake((self.frame.size.width - 100) / 2, tipCloseButton.frame.origin.y + tipCloseButton.frame.size.height, 100, self.frame.size.height - tipCloseButton.frame.size.height)];
    textView.backgroundColor = [UIColor clearColor];
    textView.textColor = [UIColor whiteColor];
    textView.text = @"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
    
    [self addSubview:textView];
    [textView release];
    
    isOpen = YES;
    
    return self;
}

- (void)dealloc
{
    
    [super dealloc];
}

#pragma mark - Methods

- (CGRect)frameForTipTollBar
{
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    CGRect viewFrame = CGRectZero;
    
    viewFrame.size.width = screenFrame.size.width;
    viewFrame.size.height = 150;
    viewFrame.origin.x = 0;
    viewFrame.origin.y = 0 + (screenFrame.size.height - viewFrame.size.height);
    
    return viewFrame;
}

- (void)openTip
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDidStopSelector:@selector(stopped:)];
    [UIView setAnimationDelegate:self];
    
    self.frame = openFrame;
    
    [UIView commitAnimations];
}

- (void)closeTip
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationDidStopSelector:@selector(stopped:)];
    [UIView setAnimationDelegate:self];
    
    self.frame = closeFrame;
    
    [UIView commitAnimations];
}

#pragma mark - IBAction Methods

- (IBAction)showAndHide:(id)sender
{
    if (isOpen)
        [self closeTip];
    else
        [self openTip];
    
    isOpen = !isOpen;
}

- (IBAction)stopped:(id)sender
{
    NSLog(@"Stopped");
}

@end
